﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InventoryMIS2019
{
    public partial class _Customer : InventoryMIS2019.Templates.BaselnfoForm
    {
        private CustomerBLL bl;
        private Customer c;
        private Contact contact;
        private List<Customer> list;
        public _Customer()
        {
            InitializeComponent();
            bl = new CustomerBLL();
            dataCustomer.AutoGenerateColumns = false;//使清空DataGridView控件数据源时，标题内容不被清空
            BindData();//绑定DataGridView的数据源
            ControlOpera(false);
        }
        private void BindData()
        {
            dataCustomer.DataSource = null;//清空DataGridView的数据源
            list = bl.GetList();
            dataCustomer.DataSource = list;//将list集合数据绑定到控件dgvCustomer作为数据源
        }
        /// <summary>
        /// 定义单击 “新增/修改/删除”按钮式，GroupBox控件中TextBox的可操作性
        /// </summary>
        /// <param name="value"></param>
        private void ControlOpera(bool value)
        {
            CustexCustomerID.Enabled = value;
            CustexCustomerName.Enabled = value;
            CustexAddress.Enabled = value;
            CustexZipCode.Enabled = value;
            CustexTel.Enabled = value;
            CustexFax.Enabled = value;
            CustexBankName.Enabled = value;
            CustexBankAccount.Enabled = value;
            CustexContacter.Enabled = value;
            CustexEmail.Enabled = value;
        }
        #region 
        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }
        #endregion
        private string Validation()
        {
            if (CustexCustomerID.Text.Trim() == "")
            {
                CustexCustomerID.Focus();//将光标定位值产品类型编号输入框
                return "客户编号不能为空！";
            }
            if (CustexCustomerName.Text.Trim() == "")
            {
                CustexCustomerName.Focus();//将光标定位值产品类型名称输入框
                return "客户名称不能为空！";
            }
            if (CustexSpellingCode.Text.Trim() == "")
            {
                CustexSpellingCode.Focus();//将光标定位值产品类型编号输入框
                return "拼音不能为空！";
            }
            if (CustexAddress.Text.Trim() == "")
            {
                CustexAddress.Focus();//将光标定位值产品类型名称输入框
                return "地址不能为空！";
            }
            if (CustexZipCode.Text.Trim() == "")
            {
                CustexZipCode.Focus();//将光标定位值产品类型编号输入框
                return "邮编不能为空！";
            }
            if (CustexTel.Text.Trim() == "")
            {
                CustexTel.Focus();//将光标定位值产品类型名称输入框
                return "电话不能为空！";
            }
            if (CustexBankName.Text.Trim() == "")
            {
                CustexBankName.Focus();//将光标定位值产品类型编号输入框
                return "银行名称不能为空！";
            }
            if (CustexBankAccount.Text.Trim() == "")
            {
                CustexBankAccount.Focus();//将光标定位值产品类型编号输入框
                return "银行账号不能为空！";
            }
            if (CustexContacter.Text.Trim() == "")
            {
                CustexContacter.Focus();//将光标定位值产品类型名称输入框
                return "联系人不能为空！";
            }
            return "";
        }
        
      
        /// <summary>
        /// 将业务实体内容显示到TextBox控件中
        /// </summary>
        private void ShowInfo()
        {
            CustexCustomerID.Text = c.CustomerID.Trim();
            CustexCustomerName.Text = c.CustomerName.Trim();
            CustexSpellingCode.Text = c.ContactInfo.SpellingCode.Trim();
            CustexAddress.Text = c.ContactInfo.Address.Trim();
            CustexZipCode.Text = c.ContactInfo.ZipCode.Trim();
            CustexTel.Text = c.ContactInfo.Tel.Trim();
            CustexFax.Text = c.ContactInfo.Fax.Trim();
            CustexBankName.Text = c.ContactInfo.BankName.Trim();
            CustexBankAccount.Text = c.ContactInfo.BankAccount.Trim();
            CustexContacter.Text = c.ContactInfo.Contacter.Trim();
            CustexEmail.Text = c.ContactInfo.Email.Trim();
        }
        /// <summary>
        /// 获取TextBox控件内容，并完成Customer实例化
        /// </summary>
        private Customer SetValue()
        {
            c = new Customer();
            c.CustomerID = CustexCustomerID.Text.Trim();
            c.CustomerName = CustexCustomerName.Text.Trim();
            contact = new Contact();
            contact.SpellingCode = CustexSpellingCode.Text.Trim();
            contact.Address = CustexAddress.Text.Trim();
            contact.ZipCode = CustexZipCode.Text.Trim();
            contact.Tel = CustexTel.Text.Trim();
            contact.Fax = CustexFax.Text.Trim();
            contact.BankName = CustexBankName.Text.Trim();
            contact.BankAccount = CustexBankAccount.Text.Trim();
            contact.Contacter = CustexContacter.Text.Trim();
            contact.Email = CustexEmail.Text.Trim();
            c = new Customer(c, contact);
            return c;
        }
        private void TemInventory_Click(object sender, EventArgs e)
        {
            string errorString = Validation();
            if (errorString.Length > 0) { MessageBox.Show(errorString, "系统提示"); return; }
            if (opera == Operation.INSERT)
            {
                SetValue();//获取控件值并完成对象赋值
                if (bl.Insert(c))
                {
                    BindData();//绑定DataGridView的数据源                    
                    Clear();//清空TextBox控件内容
                    MessageBox.Show("新增数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("新增数据失败！", "系统提示");
            }
            if (opera == Operation.UPDATE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Update(c))
                    {
                        BindData();//绑定DataGridView的数据源
                        ShowInfo();
                       
                        MessageBox.Show("成功更新数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("更新数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要修改的记录", "系统提示");
            }
            if (opera == Operation.DELETE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Delete(c))
                    {
                        BindData();//绑定DataGridView的数据源
                        
                        Clear();//清空TextBox控件内容
                        MessageBox.Show("成功删除数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("删除数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要删除的记录", "系统提示");
            }
        }    
        /// <summary>
        /// 处理关联关系类的DataGridView控件显示内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>      

        
    
    private void TemNew_Click(object sender, EventArgs e)
        {
            ControlOpera(true);
            CustexSpellingCode.Enabled = false;
        }
        /// <summary>
        /// 修改 按钮单击事件
        /// </summary>
        private void TemModify_Click(object sender, EventArgs e)
        {
            ControlOpera(true);
            CustexCustomerID.Enabled = false;
            CustexSpellingCode.Enabled = false;
            if (c != null)
                ShowInfo();
        }
        /// <summary>
        /// 删除 按钮单击事件
        /// </summary>
        private void TemDelete_Click(object sender, EventArgs e)
        {
            if (c != null)
                ShowInfo();
        }
        /// <summary>
        /// 处理关联关系类的DataGridView控件显示内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>      

        private void dataCustomer_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dataCustomer.Columns[e.ColumnIndex].Name == "SpellingCode")
                e.Value = ((Contact)e.Value).SpellingCode;
            if (dataCustomer.Columns[e.ColumnIndex].Name == "Address")
                e.Value = ((Contact)e.Value).Address;
            if (dataCustomer.Columns[e.ColumnIndex].Name == "ZipCode")
                e.Value = ((Contact)e.Value).ZipCode;
            if (dataCustomer.Columns[e.ColumnIndex].Name == "Tel")
                e.Value = ((Contact)e.Value).Tel;
            if (dataCustomer.Columns[e.ColumnIndex].Name == "Fax")
                e.Value = ((Contact)e.Value).Fax;
            if (dataCustomer.Columns[e.ColumnIndex].Name == "BankName")
                e.Value = ((Contact)e.Value).BankName;
            if (dataCustomer.Columns[e.ColumnIndex].Name == "BankAccount")
                e.Value = ((Contact)e.Value).BankAccount;
            if (dataCustomer.Columns[e.ColumnIndex].Name == "Contacter")
                e.Value = ((Contact)e.Value).Contacter;
            if (dataCustomer.Columns[e.ColumnIndex].Name == "Email")
                e.Value = ((Contact)e.Value).Email;
        }

        private void dataCustomer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dataCustomer.CurrentRow.Selected = true;
            c = list[e.RowIndex];
            if (opera == Operation.UPDATE || opera == Operation.DELETE)
                ShowInfo();
        }

        private void CustexCustomerName_TextChanged(object sender, EventArgs e)
        {
            CustexSpellingCode.Text = PY.HZToSpell(CustexCustomerName.Text.Trim());
        }
    }
}
